> ## Clayton, Horrillo, and Sniderman > ## The BIAT and the AMP As Measures of Racial Prejudice in Political Science: A Methodological Assessment> ## Step 2: Read and clean time series data> > # Initial settings --------------------------------------------------------> > rm(list = ls())> library(tidyverse)── Attaching packages ──────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.1 ──✓ ggplot2 3.3.5     ✓ purrr   0.3.4✓ tibble  3.1.6     ✓ dplyr   1.0.8✓ tidyr   1.2.0     ✓ stringr 1.4.0✓ readr   2.1.2     ✓ forcats 0.5.1── Conflicts ─────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──x dplyr::filter() masks stats::filter()x dplyr::lag()    masks stats::lag()> library(readstata13)> library(haven)> > > # Read data and calculate AMP scores  -------------------------------------> > anes.amp <- read.dta13("data/anes_timeseries_2008.dta") %>% +   select(V080001, V085311:V085360b) %>% +   filter(V085360a == 1) %>% # exclude those with a visual impairment that prevented them from completing+   mutate(V085311 = ifelse(V085311 < 0, NA, V085311), # make negative values NA +          V085335 = ifelse(V085335 < 0, NA, V085335)) %>%+   select(-contains("a")) %>% +   select(-contains("b")) %>% +   select(-contains("c")) %>% +   select(-c(V085359))Warning messages:1: In read.dta13("data/anes_timeseries_2008.dta") :    Factor codes of type double or float detected in variables   V080102, V080102a, V081102, V081103, V081103a, V081104, V081105,   V081107, V081108e, V081108f, V081108g, V081108h, V081109, V081109a,   V081110, V081111b, V081111c, V081111d, V081111f, V081111g, V081112a,   V081112b, V081112c, V081112d, V081112e, V081112f, V081112g, V081113a,   V081113b, V081113c, V081113d, V081113e, V081113f, V081113g, V081114a,   V081114b, V081114c, V081114d, V081114e, V081114f, V081114g, V081115a,   V081115b, V081115c, V081115d, V081115e, V081115f, V081115g, V081116a,   V081116b, V081116c, V081116d, V081116e, V081116f, V081116g, V081117a,   V081117b, V081117c, V081117d, V081117e, V081117f, V081117g, V081118a,   V081118b, V081118c, V081118d, V081118e, V081118f, V081118g, V081201b,   V081202b, V081203b, V081214a, V082001a, V082001b, V082002a, V082002b,   V082003a, V082003b, V082005, V082006, V082007, V082008, V082010,   V082012b, V082015, V082017b, V082020, V082021a, V082021b, V082021c,   [... truncated]2: In read.dta13("data/anes_timeseries_2008.dta") :    Missing factor labels for variables   V081111e   No labels have beend assigned.   Set option 'generate.factors=TRUE' to generate labels.> > anes.amp[anes.amp == -5] <- NA > > caseid <- anes.amp$V080001> > anes.amp <- anes.amp %>% select(-V080001)> > black <- anes.amp %>%+   select(1:24) %>% # filter black faces+   rowMeans(na.rm = TRUE) # calculate means for each respondent, ignore NA > > white <- anes.amp %>%+   select(25:48) %>% # filter white faces+   rowMeans(na.rm = TRUE) # calculate means for each respondent, ignore NA> > with.ids <- as.data.frame(cbind(caseid, black, white)) # merge back in the case id> colnames(with.ids) <- c("caseid", "black.mean", "white.mean") # add column names> > with.ids <- with.ids %>%+   mutate(diff = white.mean - black.mean) %>% +   mutate(cheater = ifelse((black.mean == 0 & white.mean == 0) | (black.mean == 1 & white.mean == 1), 1, 0))> > # Clean ANES data with additional variables -------------------------------> > anes.others <- read.dta13("data/anes_timeseries_2008.dta") %>% +   select(V080001, +          V083251a, V083251b, +          V085174a, V085174b, +          V085175a, V085175b,+          V085065c, V085064y, +          V085044a+   ) %>% +   filter(V083251a == 50 & V083251b == -1) %>% # white only+   rename(caseid = V080001,+          vote = V085044a) %>% +   mutate_if(is.factor, as.numeric) %>% +   select(-c(V083251a, V083251b))Warning messages:1: In read.dta13("data/anes_timeseries_2008.dta") :    Factor codes of type double or float detected in variables   V080102, V080102a, V081102, V081103, V081103a, V081104, V081105,   V081107, V081108e, V081108f, V081108g, V081108h, V081109, V081109a,   V081110, V081111b, V081111c, V081111d, V081111f, V081111g, V081112a,   V081112b, V081112c, V081112d, V081112e, V081112f, V081112g, V081113a,   V081113b, V081113c, V081113d, V081113e, V081113f, V081113g, V081114a,   V081114b, V081114c, V081114d, V081114e, V081114f, V081114g, V081115a,   V081115b, V081115c, V081115d, V081115e, V081115f, V081115g, V081116a,   V081116b, V081116c, V081116d, V081116e, V081116f, V081116g, V081117a,   V081117b, V081117c, V081117d, V081117e, V081117f, V081117g, V081118a,   V081118b, V081118c, V081118d, V081118e, V081118f, V081118g, V081201b,   V081202b, V081203b, V081214a, V082001a, V082001b, V082002a, V082002b,   V082003a, V082003b, V082005, V082006, V082007, V082008, V082010,   V082012b, V082015, V082017b, V082020, V082021a, V082021b, V082021c,   [... truncated]2: In read.dta13("data/anes_timeseries_2008.dta") :    Missing factor labels for variables   V081111e   No labels have beend assigned.   Set option 'generate.factors=TRUE' to generate labels.> > anes.others[anes.others < 0] <- NA> > # Merge everything --------------------------------------------------------> > all.merged <- left_join(anes.others, with.ids, by = "caseid") # merge, keep complete cases only> > all.merged <- all.merged %>%+   filter(!is.na(diff)) %>% +   mutate(obama = ifelse(vote == 1, 1, 0)) %>% +   mutate(intelldiff = V085175a - V085175b, +          lazydiff = V085174a - V085174b,+          thermdiff = V085065c - V085064y) > > all.merged.final <- all.merged %>% +   select(diff, thermdiff, lazydiff, intelldiff, cheater) %>% +   drop_na() %>% +   filter(cheater == 0)> > # Obama/AMP correlation ---------------------------------------------------> > cor(all.merged$diff, all.merged$obama, use = "na.or.complete")[1] -0.1376177> > > # Save data ---------------------------------------------------------------> > write.csv(all.merged.final, "output/main_time.csv")